<?php

namespace common\models\business;

use Yii;

/**
 * This is the model class for table "business_disease".
 *
 * @property string $id
 * @property integer $pigid
 * @property string $person
 * @property integer $personid
 * @property integer $type
 * @property string $detail
 * @property integer $farmerid
 * @property integer $createtime
 */
class BusinessDisease extends \yii\db\ActiveRecord
{

    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '疾病',      //用于修改了什么的显示
        'mainfield' => 'id',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];


    const WAIT_DIAGNOSTICS = 0;
    const DIAGNOSTICS_ING = 1;
    const PATIENTS_ING = 2;
    const RECOVERED = 3;
    public static $steps = [
        self::WAIT_DIAGNOSTICS => '待诊断',
        self::DIAGNOSTICS_ING => '已诊断',
        self::PATIENTS_ING => '康复中',
        self::RECOVERED => '已康复',
    ];


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_disease';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pigid', 'personid', 'type', 'detail', 'farmerid', 'createtime'], 'required'],
            [['pigid', 'personid', 'farmerid', 'createtime'], 'integer'],
            [['person','step'], 'string', 'max' => 20],
            [['type'], 'string', 'max' => 30],
            [['detail'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'pigid' => '仔猪id',
            'person' => '上报人',
            'personid' => '上报人id',
            'type' => '疾病类型',
            'detail' => '详情',
            'farmerid' => '农户id',
            'createtime' => '上报时间',
            'step' => '所处阶段'
        ];
    }


    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->createtime = time();
        }

        $person = BusinessPerson::findOne($this->personid);
        $this->person = !empty($person)?$person->name:'';

        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


    public function getPig()
    {
        return $this->hasOne(BusinessPig::className(),['id'=>'pigid']);
    }
}
